package ipad.asia.dao;


import ipad.asia.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.persistence.LockModeType;
import java.util.List;

@Repository
public interface ProductDao extends JpaRepository<Product,Long> {
    @Lock(LockModeType.PESSIMISTIC_WRITE)
   Product findById(int id);  //根据id查询
   List<Product> findByNameLike(String name);  //根据局部商品名查询
   List<Product> findByKind(String kind);  //根据种类查询
   List<Product> findByPriceBetween(int down,int up);  //根据价格区间查询
   List<Product> findByHaveDiscount(int flag);  //根据是否折扣商品查询
   List<Product> findByUndercarriage(int flag); //是否下架
   List<Product> findByHaveSell(int flag);  //是否卖出
    void deleteById(int id); //通过id删除
   List<Product> findByShopId(int ShopId);

    @Modifying
    @Lock(LockModeType.OPTIMISTIC)
    @Query("update Product set number=?1 where id=?2")
    void updateNumber(int number,int id);
    @Modifying
    @Query("update Product set haveDiscount=?1 where id=?2")
    void updateDiscount(int haveDiscount,int id);
    @Modifying
    @Query("update Product set name=?1,price=?2,number=?3,color=?4,kind=?5" +
            ",haveDiscount=?6,photo=?7 where id=?8")
    void update(String name,Float price,int number,String color,String kill
            ,int haveDiscount,String photo,int id);
}
